﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Argos.Workflows;
using Argos.WorkflowServices;
using Argos.Data.Model;

namespace Argos.Activities
{
	public partial class RefreshTargetDataActivity: Activity
	{

		public RefreshTargetDataActivity()
		{
			InitializeComponent();
		}


		protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
		{
			ITargetMonitoringWorkflow monitor = this.FindParentActivity<ITargetMonitoringWorkflow>();
			IStorageService service = executionContext.GetService<IStorageService>();
			if (null == service)
				throw new InvalidOperationException(string.Format("The current workflow runtime does not contain a service that implements the '{0}' interface which is required by the current operation."));

			Target t = service.GetTarget(monitor.TargetId);

			Reflector.SetObjectProperties(monitor, t.MonitoringWorkflowProperties);

			monitor.ActiveSchedules = service.GetTargetActiveSchedules(monitor.TargetId);
			monitor.BreakSchedules = service.GetTargetBreakSchedules(monitor.TargetId);
			monitor.TargetDisabled = !t.Enabled;
			monitor.TestInterval = t.TestExecutionInterval;

			return ActivityExecutionStatus.Closed;
		}

		protected override void Initialize(IServiceProvider provider)
		{
			base.Initialize(provider);

			if (null == this.FindParentActivity<ITargetMonitoringWorkflow>())
				throw new NotSupportedException(string.Format("The activity '{0}' must be hosted in a workflow of type '{1}'.", this.GetType(), typeof(ITargetMonitoringWorkflow)));

		}

	}
}
